﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using HandBook.Data;
using Telerik.Web.UI;
using HandBook.Common;

public partial class Register : System.Web.UI.Page
{
    public bool isDuplicate = false;
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnDangKyDk_Click(object sender, EventArgs e)
    {
        string username = txtUserNameDangKy.Text.Trim();
        string email = txtEmail.Text.Trim();
        //Kiem tra xem username co trong csdl
        var userusername = from usr in HandBookData.EatDrinkDB.Users
                           where usr.UserName == username
                           select usr;
        if (userusername.Count() != 0)
        {
            Response.Write("<Script>alert('Tên Này Đã Có Người Dùng!!!')</Script>");
            isDuplicate = true;
            txtUserNameDangKy.Text = "";
            txtUserNameDangKy.Focus();
        }
        else
        {
            //Kiem tra xem Email co trong csdl
            var useremail = from usr in HandBookData.EatDrinkDB.Users
                            where usr.Email == email
                            select usr;
            if (useremail.Count() != 0)
            {
                Response.Write("<Script>alert('Email Này Đã Có Người Dùng!!!')</Script>");
                isDuplicate = true;
                txtEmail.Text = "";
                txtEmail.Focus();
            }
        }
        if (isDuplicate == false)
        {
            User users = new User();
            users.UserName = username;
            users.Password = txtPasswordDangKy.Text.Trim();
            users.Address = txtAddress.Text.Trim();
            users.Email = email;
            users.PhoneNumber = txtPhone.Text.Trim();
            HandBookData.EatDrinkDB.Users.InsertOnSubmit(users);
            HandBookData.EatDrinkDB.SubmitChanges();
            Response.Write("<Script>alert('Bạn Đã Đăng Ký Thành Công!!!')</Script>");
            (form1.FindControl("RadTabStrip1") as RadTabStrip).SelectedIndex = 1;
            (form1.FindControl("RadMultiPage1") as RadMultiPage).SelectedIndex = 1;
        }
    }
    protected void btnDangNhapDN_Click(object sender, EventArgs e)
    {
        var user = from usr in HandBookData.EatDrinkDB.Users
                   where usr.UserName == txtUsernameLogin.Text.Trim() && usr.Password == txtPasswordLogin.Text.Trim()
                   select usr;
        if (user.Count() != 0)
        {
            SessionManager.CurrentUser = user.Single<User>();
            ScriptManager.RegisterStartupScript(this, GetType(), "closeWnd", "ReloadPage();", true);
        }
    }
    protected void btnQuenMatKhau_Click(object sender, EventArgs e)
    {
        if (txtUserNameQMK.Text.Trim() != string.Empty && txtEmailQMK.Text.Trim() != string.Empty)
        {
            string UserNameQMK = txtUserNameQMK.Text.Trim();
            string EmailQMK = txtEmailQMK.Text.Trim();
            //kiem tra xem username va email
            var userQMK = from usr in HandBookData.EatDrinkDB.Users
                          where usr.UserName == UserNameQMK && usr.Password == EmailQMK
                          select usr;
            if (userQMK.Count() != 0)
            {
                //Gửi Mật Khẩu Cho memeber Vào Email
            }
        }
    }
    //protected void LinkButton1_Click(object sender, EventArgs e)
    //{
    //    (form1.FindControl("RadTabStrip1") as RadTabStrip).SelectedIndex = 2;
    //    (form1.FindControl("RadMultiPage1") as RadMultiPage).SelectedIndex = 2;        
    //}
}
